﻿using AutoMapper;
using DDD.Application;
using Microsoft.Extensions.Logging;
using OrderModule.Domain.OrderAggregate;
using OrderModule.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderModule.Application.Commands
{
    public record CancelIOrderCommand(int OrderId, int GoodsId) : ICommand<Domain.OrderAggregate.Order>;

    public class CancelIOrderCommandHandler(
        IMapper mapper,
        OrderRepository repository,
        ILogger<CancelIOrderCommandHandler> logger
        )
        : ICommandHandler<CancelIOrderCommand, Domain.OrderAggregate.Order>
    {
        public async Task<Order> Handle(CancelIOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await repository.GetAsync(request.OrderId) ?? throw new Exception($"找不到ID为{request.OrderId}的订单");
            order.ChangeStatus("库存不够");
            return order;
        }
    }
}
